package com.arsl.client.view;

import java.util.ArrayList;
import java.util.List;

import com.arsl.client.data.Content;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Style.FontWeight;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;

public class ViewPageButton extends Button implements ClickHandler {

	private int id;
	private Content[] contentList;
	private int imgWidth;
	private int imgHeight;

	public ViewPageButton() {
		this.addClickHandler(this);
		this.getElement().getStyle().setBorderColor("blue");
	}

	@Override
	public void onClick(ClickEvent event) {

		if (contentList.length == 1) {
			Window.Location.assign(contentList[0].getUrl());
		} else {
			final PopupPanel pop = new PopupPanel();
			pop.setStyleName("popup");
			VerticalPanel panel = new VerticalPanel();
			panel.add(getList());
			panel.setWidth(Float.toString((float) (imgWidth / 1.5)) + "px");
			// panel.setHeight(Float.toString(imgHeight / 8) + "px");
			Button btn = new Button();
			btn.setWidth("100%");
			btn.setText("Cancel");
			btn.getElement().getStyle().setFontSize(40, Unit.PX);
			btn.setStyleName("cancel-button");
			btn.addClickHandler(new ClickHandler() {

				@Override
				public void onClick(ClickEvent event) {
					pop.hide();
				}
			});
			panel.add(btn);
			pop.setWidget(panel);
			pop.setPopupPosition(this.imgWidth / 2 - this.imgWidth / 3,
					this.imgHeight / 2 - this.imgHeight / 16);
			pop.setGlassEnabled(true);
			pop.show();
		}

	}

	private CellTable<Content> getList() {
		/*
		 * List<Content> list = java.util.Arrays.asList(getContentList());
		 * ContentCell textCell = new ContentCell();
		 * 
		 * CellList<Content> cellList = new CellList<Content>(textCell);
		 * 
		 * cellList.getElement().getStyle().setBorderWidth(10, Unit.PX);
		 * cellList.setRowCount(list.size(), true); cellList.setRowData(0,
		 * list);
		 * 
		 * return cellList;
		 */

		// List<Content> list = java.util.Arrays.asList(getContentList());
		CellTable<Content> table = new CellTable<Content>();

		TitleCell titleCell = new TitleCell();
		Column<Content, Content> titleColumn = new Column<Content, Content>(
				titleCell) {
			@Override
			public Content getValue(Content object) {
				return object;
			}
		};

		ImageCell imageCell = new ImageCell();
		Column<Content, Content> imageColumn = new Column<Content, Content>(
				imageCell) {
			@Override
			public Content getValue(Content object) {
				return object;
			}
		};

		table.addColumn(imageColumn);
		table.addColumn(titleColumn);
		table.setColumnWidth(1, 100, Unit.PCT);
		ListDataProvider<Content> dataProvider = new ListDataProvider<Content>();

		// Connect the table to the data provider.
		dataProvider.addDataDisplay(table);

		// Add the data to the data provider, which automatically pushes it to
		// the
		// widget.
		List<Content> list = dataProvider.getList();
		for (Content contact : getContentList()) {
			list.add(contact);
		}

		table.getElement().getStyle().setFontSize(40, Unit.PX);
		table.getElement().getStyle().setFontWeight(FontWeight.BOLD);
		return table;
	}

	private abstract class ContentCell extends AbstractCell<Content> {

		public ContentCell() {
			super("click", "keydown");

		}

		@Override
		public void onBrowserEvent(Context context, Element parent,
				Content value, NativeEvent event,
				ValueUpdater<Content> valueUpdater) {
			// Check that the value is not null.
			if (value == null) {
				return;
			}

			// Call the super handler, which handlers the enter key.
			super.onBrowserEvent(context, parent, value, event, valueUpdater);

			// On click, perform the same action that we perform on enter.
			if ("click".equals(event.getType())) {
				this.onEnterKeyDown(context, parent, value, event, valueUpdater);
			}
		}

		@Override
		protected void onEnterKeyDown(Context context, Element parent,
				Content value, NativeEvent event,
				ValueUpdater<Content> valueUpdater) {
			// Window.alert("You clicked " + value.getUrl());
			if ("Web Page".equalsIgnoreCase(value.getType())
					|| "Video".equalsIgnoreCase(value.getType())) {
				Window.Location.assign(value.getUrl());
			}
		}

	}

	private class ImageCell extends ContentCell {

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context,
				Content value, SafeHtmlBuilder sb) {
			/*
			 * if (value == null) { return; } sb.appendHtmlConstant(
			 * "<div style=\"size:200%;font-size:40px;font-weight:bold;padding:10px;border-bottom: 5px solid #9F9F9F;\">"
			 * ); sb.appendEscaped(value.getTitle());
			 * sb.appendHtmlConstant("</div>");
			 */

			if (value.getType().equals("Web Page")) {
				sb.appendHtmlConstant("<img width=\"40\" src=\"http://www.paperazi.com/img/web_icon.png\">");
			} else if (value.getType().equals("Video")) {
				sb.appendHtmlConstant("<img width=\"40\" src=\"http://www.paperazi.com/img/video_icon.png\">");
			}

		}

	}

	private class TitleCell extends ContentCell {

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context,
				Content value, SafeHtmlBuilder sb) {

			if (value == null) {
				return;
			}
			sb.appendHtmlConstant("<div style=\"size:200%;font-size:40px;font-weight:bold;padding:10px;\">");
			sb.appendEscaped(value.getTitle());
			sb.appendHtmlConstant("</div>");
			// border-bottom: 5px solid #9F9F9F;
		}

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Content[] getContentList() {
		return contentList;
	}

	public void setContentList(Content[] contentList) {
		this.contentList = contentList;
	}

	private List<String> getContentUrlList() {
		List<String> list = new ArrayList<String>();
		for (Content cnt : this.contentList) {
			list.add(cnt.getUrl());
		}
		return list;

	}

	public int getImgWidth() {
		return imgWidth;
	}

	public void setImgWidth(int imgWidth) {
		this.imgWidth = imgWidth;
	}

	public int getImgHeight() {
		return imgHeight;

	}

	public void setImgHeight(int ingHeight) {
		this.imgHeight = ingHeight;
	}

}
